package edu.bw.Config;
/*
该类作为Spring的配置类 目的是取代Spring的配置文件（applicationContext.xml）
 */

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

@Configuration//代表这是一个Spring配置类，相当于将xml中配置转化成java类和注解的形式
@ComponentScan("edu.bw")//扫描包及子包下的注释，
@PropertySource("classpath:db.properties")
public class SpringConfig {

    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.user}")
    private String user;


    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DataSource initDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }


    @Bean("queryRunner")
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public QueryRunner initQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);

    }


}
